package com.yjy.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjy.entity.Account;
import com.yjy.entity.Category;
import com.yjy.entity.Introduction;
import com.yjy.entity.User;
import com.yjy.exception.CustomerException;
import com.yjy.mapper.CategoryMapper;
import com.yjy.mapper.IntroductionMapper;
import com.yjy.mapper.UserMapper;
import com.yjy.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IntroductionService {
    @Resource
    IntroductionMapper introductionMapper;

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    UserMapper userMapper;

    public Introduction selectById(String userId) {
        return introductionMapper.selectById(userId);
    }


    public List<Introduction> selectAll(Introduction introduction){
        List<Introduction> introductions = introductionMapper.selectAll(introduction);
        for (Introduction dbIntroduction : introductions) {
            dbIntroduction.setDescription(HtmlUtil.cleanHtmlTag(dbIntroduction.getContent()));
        }
        return introductions;
    }

    public PageInfo<Introduction> selectPage(Integer pageNum, Integer pageSize,Introduction introduction) {
        //查之前先给他条件
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            introduction.setUserId(currentUser.getId());
        }
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Introduction> introductions = introductionMapper.selectAll(introduction);
//        for (Introduction dbintroduction : introductions) {
//            Integer categoryId = dbintroduction.getCategoryId();
//            Integer userId = dbintroduction.getUserId();
//            Category category = categoryMapper.selectById(categoryId);
//            User user = userMapper.selectById(userId.toString());
//
//            if(ObjectUtil.isNotEmpty(category)){
//                dbintroduction.setCategoryTitle(category.getTitle());
//            }
//
//            if(ObjectUtil.isNotEmpty(user)){
//                dbintroduction.setUsername(user.getName());
//            }
//        }
        return PageInfo.of(introductions);
    }

    public void add(Introduction introduction) {
        Account currentUser = TokenUtils.getCurrentUser();
        introduction.setUserId(currentUser.getId());
        introduction.setTime(DateUtil.now());
        introductionMapper.insert(introduction);
    }

    public void update(Introduction introduction) {

        introductionMapper.updateById(introduction);
    }

    public void deleteById(Integer id) {
        introductionMapper.deleteById(id);
    }



}






















